package com.oracle.zebrafreelytraveling.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResBase;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResCard;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResCircle;
import com.oracle.zebrafreelytraveling.data.bean.Card;
import com.oracle.zebrafreelytraveling.data.bean.User;
import com.oracle.zebrafreelytraveling.net.HttpManager;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.CommonAdapter;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.MultiItemTypeAdapter;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.base.ViewHolder;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.wrapper.LoadMoreWrapper;
import com.oracle.zebrafreelytraveling.ui.adapter.CardsListAdapter;
import com.oracle.zebrafreelytraveling.utils.ConvertUtils;
import com.oracle.zebrafreelytraveling.utils.StringUtil;
import com.oracle.zebrafreelytraveling.utils.ToastUtil;
import com.oracle.zebrafreelytraveling.utils.UserUtil;
import com.oracle.zebrafreelytraveling.utils.constant.Constant;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by yydcnjjw on 2017/8/10.
 */

public class MyCardsActivity extends BaseActitvity {

    private RecyclerView mCardsRecycle;
    private CommonAdapter<Card> mCardsAdapter;
    private List<Card> mCardsList;
    private LoadMoreWrapper mLoadMoreWrapper;

    private int page = 0;
    private boolean isListNull = false;
    private ViewHolder mLoadMoreHolder;
    private Disposable mDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initRecycleView();
    }

    private void initRecycleView(){
        mCardsRecycle = (RecyclerView) findViewById(R.id.cards_recycle_view);
        mCardsRecycle.setItemAnimator(new DefaultItemAnimator());
        mCardsRecycle.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mCardsRecycle.setLayoutManager(new LinearLayoutManager(this));
        mCardsList = new ArrayList<>();
        mCardsAdapter = new CommonAdapter<Card>(this, R.layout.item_personal_cards, mCardsList) {
            @Override
            protected void convert(ViewHolder holder, Card card, int position) {
                holder.setText(R.id.personal_card_content, card.getContent());
                holder.setText(R.id.personal_card_time, ConvertUtils.getNormalTimeFormat(card.getTime()));
                holder.setText(R.id.personal_card_receive, card.getReceive()+"");
            }
        };
        mCardsAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                User user = UserUtil.getUserInfoLocalBySP();
                if (user == null){
                    return;
                }
                ResCard.DataBean card = new ResCard.DataBean();
                card.setHeadImg(user.getHeadImg());
                card.setUserName(user.getName());
                card.setCard(mCardsList.get(position));
                CardInfoActivity.startCardInfoActivity(MyCardsActivity.this, card);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mLoadMoreWrapper = new LoadMoreWrapper(mCardsAdapter);
        mLoadMoreWrapper.setLoadMoreView(R.layout.footer_loading);
        mLoadMoreWrapper.setOnLoadMoreListener(viewHolder -> {
            mLoadMoreHolder = viewHolder;
            if (!isListNull){
                loadCardsList();
            } else {
                viewHolder.getConvertView().setVisibility(View.GONE);
            }
        });
        mCardsRecycle.setAdapter(mLoadMoreWrapper);
    }

    private void loadCardsList(){
        User user = UserUtil.getUserInfoFromSP();
        if (user == null){
            return;
        }
        HttpManager.getInstance().getService(Constant.BASE_URL)
                .getCardsByUserId(user.getUserId(), page, Constant.MAX_PAGE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResBase<List<Card>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(@NonNull ResBase<List<Card>> resCard) {
                        if (resCard.getCode().equals(Constant.REQUEST_SUCCESS)){
                            mCardsList.addAll(resCard.getData());
                            isListNull = false;
                        } else {
                            ToastUtil.showToast(getString(R.string.not_any_more));
                            isListNull = true;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("loadCircleList", e.getMessage());
                        mLoadMoreHolder.getConvertView().setVisibility(View.GONE);
                    }

                    @Override
                    public void onComplete() {
                        mLoadMoreHolder.getConvertView().setVisibility(View.GONE);
                        mLoadMoreWrapper.notifyDataSetChanged();
                    }
                });
        page++;
    }

    public static void startMyCardsActivity(Activity activity){
        activity.startActivity(new Intent(activity, MyCardsActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //清除 Disposable
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
        page = 0;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_cards;
    }
}
